package com.xmtlxx.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.domain.entity.SysForm;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmtlxx.system.domain.bo.SysFormBo;
import com.xmtlxx.system.domain.vo.SysCheckItemVo;
import com.xmtlxx.system.domain.vo.SysCheckTypeVo;
import com.xmtlxx.system.domain.vo.SysFormVo;
import com.xmtlxx.system.mapper.SysFormMapper;
import com.xmtlxx.system.service.ISysFormService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 业务单Service业务层处理
 *
 * @author xmtlxx
 * @date 2022-04-24
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class SysFormServiceImpl implements ISysFormService {

    private final SysFormMapper baseMapper;

    /**
     * 查询业务单
     *
     * @param formid 业务单主键
     * @return 业务单
     */
    @Override
    public SysFormVo queryById(String formid){
        return baseMapper.selectVoById(formid);
    }

    /**
     * 查询业务单列表
     *
     * @param bo 业务单
     * @return 业务单
     */
    @Override
    public TableDataInfo<SysFormVo> queryPageList(SysFormBo bo, PageQuery pageQuery) {
       // LambdaQueryWrapper<SysForm> lqw = buildQueryWrapper(bo);
        Page<SysFormVo> result = baseMapper.selectPageSysFormList(pageQuery.build(),bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询业务单列表
     *
     * @param bo 业务单
     * @return 业务单
     */
    @Override
    public List<SysFormVo> queryList(SysFormBo bo) {
        //LambdaQueryWrapper<SysForm> lqw = buildQueryWrapper(bo);
        return baseMapper.selectSysFormList(bo);
    }

    private LambdaQueryWrapper<SysForm> buildQueryWrapper(SysFormBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysForm> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getMid()), SysForm::getMid, bo.getMid());
        lqw.eq(bo.getTypeId() != null, SysForm::getTypeId, bo.getTypeId());
        lqw.eq(bo.getItemId() != null, SysForm::getItemId, bo.getItemId());
        return lqw;
    }

    /**
     * 新增业务单
     *
     * @param bo 业务单
     * @return 结果
     */
    @Override
    public Boolean insertByBo(SysFormBo bo) {
        SysForm add = BeanUtil.toBean(bo, SysForm.class);
        validEntityBeforeSave(add);

        //填充UUID
        add.setFormid(IdUtil.simpleUUID());

        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setFormid(add.getFormid());
        }
        return flag;
    }

    /**
     * 修改业务单
     *
     * @param bo 业务单
     * @return 结果
     */
    @Override
    public Boolean updateByBo(SysFormBo bo) {
        SysForm update = BeanUtil.toBean(bo, SysForm.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(SysForm entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除业务单
     *
     * @param ids 需要删除的业务单主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<SysCheckTypeVo> loadModelItems(String modelid) {
        SysFormBo bo = new SysFormBo();
        bo.setMid(modelid);
        return baseMapper.selectModelSysFormList(bo);
    }

    @Override
    public List<SysCheckItemVo> selectSysFormItemsList(SysFormBo sysForm) {
        return baseMapper.selectSysFormItemsList(sysForm);
    }
}
